home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
BARNET
/
FREENET
/
BRODIE
/
INTERNET
/
!InternetD
/
c
/
udp_test
< prev
Wrap
Text File
|
1995-06-08
|
4KB
|
137 lines
/* udp_test.c */
#define socketclose close
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "inetd.h"
const int verbose = 0;
static void wait_for_data(int s)
{
fd_set f;
struct timeval timeout;
do {
FD_ZERO(&f); FD_SET(s, &f);
timerclear(&timeout);
} while (select(FD_SETSIZE, &f, NULL, NULL, &timeout)==0);
if (verbose) printf("data has arrived.\n");
}
int s;
struct sockaddr_in s_in, localhost;
int size = 16, sz;
char buf[512];
void port_daytime(void)
{
s_in = localhost;
s_in.sin_port = htons(13);
sendto(s, buf, 1, 0, (struct sockaddr *)&s_in, 16);
wait_for_data(s);
sz = 16;
size = recvfrom(s, buf, 512, 0, (struct sockaddr *)&s_in, &sz);
if (size>0) {
buf[size] = '\0';
printf("%s", buf);
}
}
void port_chargen(void)
{
if (verbose) printf("chargening\n");
buf[0] = '\0';
s_in = localhost; s_in.sin_port = htons(19);
if (sendto(s, buf, 1, 0, (struct sockaddr *)&s_in, 16) == -1) {
printf("problem with sendto\n");
}
if (verbose) printf("waiting for response\n");
wait_for_data(s);
sz = 16;
s_in = localhost; s_in.sin_port = htons(7);
size = recvfrom(s, buf, 512, 0, (struct sockaddr *)&s_in, &sz);
if (size>0) { buf[size] = '\0'; printf("%s\n", buf); }
}
void port_echo(void)
{
if (verbose) printf("echoing\n");
strcpy(buf, "0123456789--Data--to--Echo--9876543210");
s_in = localhost; s_in.sin_port = htons(7);
if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&s_in, 16) == -1) {
printf("problem with sendto\n");
}
if (verbose) printf("waiting for response\n");
wait_for_data(s);
sz = 16;
s_in = localhost; s_in.sin_port = htons(7);
size = recvfrom(s, buf, 512, 0, (struct sockaddr *)&s_in, &sz);
if (size>0) { buf[size] = '\0'; printf("%s\n", buf); }
}
void port_syslog(void)
{
if (verbose) printf("syslogging\n");
strcpy(buf, "<--syslog'ed datagram-->");
s_in = localhost; s_in.sin_port = htons(514);
if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&s_in, 16) == -1) {
printf("problem with sendto(syslog)\n");
}
}
void port_rwall(void)
{
if (verbose) printf("rwalling\n");
strcpy(buf, "<--rwall'ed message-->");
s_in = localhost; s_in.sin_port = htons(533);
if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&s_in, 16) == -1) {
printf("problem with sendto(syslog)\n");
}
}
int prompt(char *message)
{
char *r;
printf("%s (Y/N/count) ? ", message);
r = fgets(buf, 512, stdin);
if (!r) return 0;
if (isdigit(*r)) return atoi(r);
return toupper(*r) == 'Y';
}
int main(int argc, char **argv)
{
int loop;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
fprintf(stderr, "Unable to create\n");
return 1;
}
localhost.sin_family = AF_INET;
localhost.sin_port = 0;
localhost.sin_addr.s_addr = 0x0100007f;
strcpy(localhost.sin_zero, "\0\0\0\0\0\0\0");
/*-----*/
for (loop=prompt("daytime"); loop>0; --loop) port_daytime();
for (loop=prompt("chargen"); loop>0; --loop) port_chargen();
for (loop=prompt("echo" ); loop>0; --loop) port_echo();
for (loop=prompt("syslog" ); loop>0; --loop) port_syslog();
for (loop=prompt("rwall" ); loop>0; --loop) port_rwall();
/*-----*/
shutdown(s,2);
close(s);
return 0;
}